﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace BrowserPL3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<TabItem> _tabItems;
        private List<TabStructure> _tabContents;
        private bool _WindowMaximized = true;

        BrowserMainWindow _window;

        //////
        /// <summary>
        /// MainWindow Constractor
        /// <remarks>
        /// instalize new object from BrowserMainWindow
        /// add first Tab to window 
        /// set first tab index =0
        /// </remarks>
        /// </summary>
        public MainWindow()
        {
            try
            {

                InitializeComponent();

                _window = new BrowserMainWindow();

                _window.AddTabItem();
                MainControlTab.DataContext = _window._tabItems;
                MainControlTab.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }





        /// <summary>
        /// close the peogram
        /// <remarks>
        /// if tab numbers >= 2 show warrning window
        /// </remarks>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void close_click(object sender, RoutedEventArgs e)
        {
            
        
            if (_window._tabItems.Count >= 2)
            {

                CloseWindow close = new CloseWindow();
                close.Show();
              

            }
            else
                App.Current.Shutdown();
        
        }

        /// <summary>
        /// maximize click function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void maxi_click(object sender, RoutedEventArgs e)
        {
            if (_WindowMaximized == false)
            {
                this.WindowState = WindowState.Maximized;
                _WindowMaximized = true;
            }
            else
            {
                this.WindowState = WindowState.Normal;
                _WindowMaximized = false;
            }
            
        }
        /// <summary>
        /// minimize click function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void mini_click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }





        /// <summary>
        /// add new tab function
        /// <remarks>
        /// it runs when new tab is clicked</remarks>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void Add_New_Tab(object sender, RoutedEventArgs e)
        {


           
            //clear tab control binding
            MainControlTab.DataContext = null;

            TabItem newTab = _window.AddTabItem();
            //newTab.HeaderTemplate = MainControlTab.FindResource("TabHeader") as DataTemplate;
            // bind tab control
            MainControlTab.DataContext = _window._tabItems;

            // select newly added tab item
            MainControlTab.SelectedItem = newTab;

            


        }
        /// <summary>
        /// close tab function it runs when close tabe button clicked
        /// </summary>
        /// <remarks>
        /// it check whether the close tab are number on or otherwise
        /// </remarks>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Close_Tab(object sender, RoutedEventArgs e)
        {
            if (_window._tabItems.Count == 1)
            {
                this.Close();
                App.Current.Shutdown();
                return;
            }
            TabItem selectedTab = MainControlTab.SelectedItem as TabItem;

            // clear tab control binding
            MainControlTab.DataContext = null;

            _window.removeTab(selectedTab);

            // bind tab control
            MainControlTab.DataContext = _window._tabItems;
            MainControlTab.SelectedItem = _window._tabItems[_window._tabItems.Count -1];
        }

        /// <summary>
        /// new window function clicked 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newWindowClick(object sender, RoutedEventArgs e)
        {
           MainWindow newWindow = new MainWindow();
           newWindow.Dispatcher.BeginInvoke
                (System.Windows.Threading.DispatcherPriority.Normal,
                (Action)(() =>
                {
                    newWindow.Show();
                    //---or do any thing u want with that form
                }
                ));
        }

        /// <summary>
        /// 
        /// new tab clicked function
        /// <remarks>
        /// it runs every tab in new thread</remarks>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newTabClick(object sender, RoutedEventArgs e)
        {
            BrowserMainWindow window = new BrowserMainWindow();
            TabStructure tab = new TabStructure();
           tab.Dispatcher.BeginInvoke
               (System.Windows.Threading.DispatcherPriority.Normal,
               (Action)(() =>
               {
                   MainControlTab.DataContext = null;

                   TabItem newTab = _window.AddTabItem();
                   //newTab.HeaderTemplate = MainControlTab.FindResource("TabHeader") as DataTemplate;
                   // bind tab control
                   MainControlTab.DataContext = _window._tabItems;

                   // select newly added tab item
                   MainControlTab.SelectedItem = newTab;
               }
               ));
           
               
           
 
          

        }

        /// <summary>
        /// 
        /// show the setting window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void settingClick(object sender, RoutedEventArgs e)
        {
           
          
                SettingWindow set = new SettingWindow();
                set.Show();
         
         }



       

        
    }
}
